package campfirelighter.services {
	import campfirelighter.events.RoomsEvent;
	import campfirelighter.model.Room;
	import campfirelighter.utils.UserPreferences;
	
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	
	import mx.collections.ArrayCollection;
	import mx.utils.Base64Encoder;
	
	/**
	 * @author Danny Patterson
	 */
	public class StreamingService extends EventDispatcher implements IService {
		
		protected var loader:URLLoader;
		protected var _roomId:uint;
		
		public function StreamingService() {}
		
		private function onComplete(event:Event):void {
			/*
			var result:XML = XML(loader.data);
			var rooms:ArrayCollection = new ArrayCollection();
			for each(var roomData:XML in result.room) {
				var room:Room = new Room();
				room.xmlDeserialize(roomData);
				rooms.addItem(room)
			}
			dispatchEvent(new RoomsEvent(RoomsEvent.ROOMS_LOADED, rooms));
			*/
		}
		
		private function onError(event:ErrorEvent):void {
			dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false, false, event.text));
		}
		
		public function setRoomId(id:uint):void {
			_roomId = id;
		}
		
		public function execute():void {
			var encodedCredentials:Base64Encoder = new Base64Encoder();
			encodedCredentials.insertNewLines = false;
			encodedCredentials.encode(UserPreferences.authtoken + ":" + UserPreferences.authpassword);
			
			var authHeader:URLRequestHeader = new URLRequestHeader("Authorization", "Basic " + encodedCredentials.flush());
			
			var request:URLRequest = new URLRequest();
			request.url = "https://streaming.campfirenow.com/room/" + _roomId + "/live.xml";
			request.requestHeaders = new Array(authHeader);
			loader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
			loader.load(request);
		}
		
	}
	
}

/*
<?xml version="1.0" encoding="UTF-8"?>
<room>
<created-at type="datetime">2010-01-25T17:48:42Z</created-at>
<id type="integer">269555</id>
<membership-limit type="integer">25</membership-limit>
<name>Engine Room</name>
<topic>Development team jamming plus updates from Beanstalk.</topic>
<updated-at type="datetime">2010-03-10T18:01:28Z</updated-at>
<open-to-guests type="boolean">false</open-to-guests>
<full type="boolean">false</full>
<users type="array">
<user type="Member">
<admin type="boolean">true</admin>
<created-at type="datetime">2009-12-02T18:08:42Z</created-at>
<id type="integer">511390</id>
<type>Member</type>
<name>Jamie Thingelstad</name>
<email-address>jamie@thingelstad.com</email-address>
</user>
<user type="Member">
<admin type="boolean">true</admin>
<created-at type="datetime">2009-12-02T18:35:47Z</created-at>
<id type="integer">511396</id>
<type>Member</type>
<name>Louis Abramowski</name>
<email-address>louis.abramowski@alvenda.com</email-address>
</user>
<user type="Member">
<admin type="boolean">false</admin>
<created-at type="datetime">2009-12-03T20:29:23Z</created-at>
<id type="integer">511786</id>
<type>Member</type>
<name>Mike Hobbs</name>
<email-address>mike.hobbs@alvenda.com</email-address>
</user>
<user type="Member">
<admin type="boolean">false</admin>
<created-at type="datetime">2009-12-11T20:22:05Z</created-at>
<id type="integer">514061</id>
<type>Member</type>
<name>Morgan Catlin</name>
<email-address>morgan.catlin@alvenda.com</email-address>
</user>
<user type="Member">
<admin type="boolean">false</admin>
<created-at type="datetime">2010-01-04T15:25:14Z</created-at>
<id type="integer">604609</id>
<type>Member</type>
<name>Mark Bradley</name>
<email-address>mbradley@alvenda.com</email-address>
</user>
<user type="Member">
<admin type="boolean">false</admin>
<created-at type="datetime">2010-02-10T16:05:53Z</created-at>
<id type="integer">620216</id>
<type>Member</type>
<name>Ira Mitchell</name>
<email-address>imitchell@alvenda.com</email-address>
</user>
<user type="Member">
<admin type="boolean">true</admin>
<created-at type="datetime">2010-02-25T17:24:40Z</created-at>
<id type="integer">626327</id>
<type>Member</type>
<name>Jeanette Dorazio</name>
<email-address>jdorazio@alvenda.com</email-address>
</user>
<user type="Member">
<admin type="boolean">false</admin>
<created-at type="datetime">2010-03-05T22:57:46Z</created-at>
<id type="integer">629587</id>
<type>Member</type>
<name>Mike Bjerkness</name>
<email-address>mbjerkness@alvenda.com</email-address>
</user>
</users>
</room>
*/